import { FNCManager, KdsMessageType } from './../utils/fnc/fncManager';
import { ConfigSvr } from './../services/config';
import { Device, Devices } from './../models/devices';
import { StoreSvr } from './../services/store';
import { HostBinding } from '@angular/core';
import { Component, ElementRef, AfterViewInit, OnDestroy, OnChanges, Input, Output, SimpleChange, EventEmitter } from '@angular/core';
import { MdDialog, MdDialogRef } from '@angular/material';

@Component({
    selector: 'exit-dialog',
    styleUrls: [
        './components.scss'
    ],
    template: `
        <div class="exit-dialog-container">
            <header>关机</header>
            <div class="content-container">
                <md-radio-group class="example-radio-group" [(ngModel)]="selected">
                    <div *ngFor="let option of options" class="option">
                        <md-radio-button class="example-radio-button"  [value]="option.type">
                            {{option.description}}
                        </md-radio-button>
                        <div *ngIf = "option.name=='exitAppAndTransfer'" class="select">
                            <select class="kds-select"  [ngModel]="selectedDeviceId">
                                <option color="primary" *ngFor="let deivce of otherDevices" [value]="deivce.id">{{deivce.name}}({{deivce.kitchenSolution&&deivce.kitchenSolution.name}})</option>
                            </select>
                        </div>
                    </div>
                </md-radio-group>
            </div>
            <div class="buttons fix"> 
                <button md-button (click)="cancel()">取消</button>
                <button md-button color="primary" (click)="confirm()">确认</button>
            </div>
        </div>
    `,
})
export class ExitDialog {
    @HostBinding('class.exit-dialog') b: boolean = true;
    constructor(private storeSvr: StoreSvr, public dialogRef: MdDialogRef<ExitDialog>, private configSvr: ConfigSvr) { }
    selected: ExitDialogMessageResultType;
    selectedDeviceId: string;
    form: {
        type: any,
        args: any
    }
    otherDevices: Array<Devices>
    async confirm() {
        this.form = {
            type: this.selected,
            args: {
                selectedDeviceId: this.selectedDeviceId
            }
        }

        const result = this.form;
        this.configSvr.lastConfig = this.form;
        this.dialogRef.close(this.form);
        switch (result && result.type) {
            case ExitDialogMessageResultType.exitApp: {
                break;
            }
            case ExitDialogMessageResultType.exitAppAndShutdown: {
                (<any>window).device.shutdown()
                //关机
                break;
            }
            case ExitDialogMessageResultType.exitAppAndTransfer: {
                const device = await this.storeSvr.transferDevice(this.storeSvr.getCurrentDeviceId(), result.args.selectedDeviceId);
                FNCManager.Current.KdsMessageNotify(KdsMessageType.DeviceMessage, device);
                break;
            }
        }
        this.exitApp();
    }
    exitApp() {
        //全屏
        (<any>window).device.exitApp();
    }

    cancel() {
        this.dialogRef.close()
    }
    async ngOnInit() {
        this.otherDevices = await this.storeSvr.getOtherDevices();
        this.selectedDeviceId = (this.configSvr.lastConfig && this.configSvr.lastConfig.selectedDeviceId) || this.otherDevices[0].id;
        this.selected = (this.configSvr.lastConfig && this.configSvr.lastConfig.type) || ExitDialogMessageResultType.exitApp;
    }

    private options = [{
        description: "退出应用",
        type: ExitDialogMessageResultType.exitApp,
        name: "exitApp"
    },
    {
        description: "关闭终端(退出应用并关机)",
        type: ExitDialogMessageResultType.exitAppAndShutdown,
        name: "exitApp"
    },
    {
        description: "关闭本终端并且转移到以下终端显示",
        type: ExitDialogMessageResultType.exitAppAndTransfer,
        name: "exitAppAndTransfer"
    }]
}




export enum ExitDialogMessageResultType {
    exitApp,
    exitAppAndShutdown,
    exitAppAndTransfer
}


